module xdb_module

    use, intrinsic :: iso_c_binding, only: new_line => c_new_line
    use fffc_module, only: exists, mkdir, operator(.join.), terminal_obj
    use xdb_command_line, only: cli_args
    implicit none

    private
    public :: touch_launch

contains

    !> touch a .vscode/launch.json file
    subroutine touch_launch()
        character(*), parameter :: json_content = &
            &'{'//new_line// &
            '    "version": "0.2.0",'//new_line// &
            '    "configurations": ['//new_line// &
            '        {'//new_line// &
            '            "name": "gfortran - Build and debug active file",'//new_line// &
            '            "type": "cppdbg",'//new_line// &
            '            "request": "launch",'//new_line// &
            '            "program": "${fileDirname}/a.out",'//new_line// &
            '            "args": [],'//new_line// &
            '            "stopAtEntry": false,'//new_line// &
            '            "cwd": "${workspaceFolder}",'//new_line// &
            '            "environment": [],'//new_line// &
            '            "externalConsole": false,'//new_line// &
            '            "MIMode": "gdb",'//new_line// &
            '        }'//new_line// &
            '    ]'//new_line// &
            '}'

        if (exists(trim(cli_args%working_dir) .join.'.vscode/launch.json') .and. .not. cli_args%skip) then
            call terminal_obj%warning("launch.json already exists")
            return
        end if

        if (.not. exists(trim(cli_args%working_dir) .join.".vscode", .true.)) call mkdir(trim(cli_args%working_dir) .join.".vscode")

        open (1, file=trim(cli_args%working_dir) .join.".vscode/launch.json")
        write (1, "(A)") json_content
        close (1)

        call terminal_obj%success("launch.json created")

    end subroutine touch_launch

end module xdb_module
